<?php
	
	// Add RSS links to <head> section
	automatic_feed_links();
    
    function init_scripts(){
        // Load jQuery
    	if ( !is_admin() ) {
    	   //wp_deregister_script('jquery');
    	   //wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"), false);
           
    	   //Registering the styles needed for the page...
           wp_register_style( 'style', get_bloginfo('stylesheet_url') , array(), false, "all" );
           wp_register_style( 'shop', get_bloginfo('stylesheet_directory')."/shopp/shopp.css" , array(), false, "all" );
           wp_register_style( 'superfish', get_bloginfo('stylesheet_directory')."/css/superfish.css" , array(), false, "all" );
           wp_register_style( 'cloudZoom', get_bloginfo('stylesheet_directory')."/css/cloud-zoom.css" , array(), false, "all" );
           wp_register_style( 'fancybox', get_bloginfo('stylesheet_directory')."/fancybox/jquery.fancybox-1.3.4.css" , array(), false, "all" );
           wp_register_style( 'home', get_bloginfo('stylesheet_directory')."/css/home.css" , array(), false, "all" );
           
           //Registering the required scripts to load in the respective places.
           wp_register_script( 'main', get_bloginfo('template_directory')."/js/main.js", 'jquery', '1.4.8', false );
           wp_register_script( 'superfish', get_bloginfo('template_directory')."/js/superfish.js", 'jquery', '1.4.8', true );
           wp_register_script( 'fancybox', get_bloginfo('template_directory')."/fancybox/jquery.fancybox-1.3.4.min.js", array('jquery'), '1.3.4', true );
           wp_register_script( 'carousel', get_bloginfo('template_directory')."/js/jquery.carouFredSel-3.2.1.js", array('jquery'), '1.0', true );
           wp_register_script( 'product', get_bloginfo('template_directory')."/js/product.js", array('jquery'), '1.0', true );
           wp_register_script( 'cloudZoom', get_bloginfo('template_directory')."/js/cloud-zoom.1.0.2.min.js", array('jquery'), '1.0.2', true );
           wp_register_script( 'currency', get_bloginfo('template_directory')."/js/currency.js", array('jquery'), '1.0', true );
           wp_register_script( 'kwickslider', get_bloginfo('template_directory')."/js/kwickslider.js", array('jquery'), '1.0', true ); 
           wp_register_script( 'easing', get_bloginfo('template_directory')."/js/jquery.easing.1.3.js", array('jquery'), '1.0', true ); 
           wp_register_script( 'home', get_bloginfo('template_directory')."/js/home.js", array('jquery'), '1.0', true );
    	}
    }
    	
	add_action('init', 'init_scripts');
    
    function add_styles(){
        if(!is_admin()){
            //enqueue the generic styles
            wp_enqueue_style('style');
            wp_enqueue_style('shop');
            wp_enqueue_style('superfish');
            
            if(is_home()){
               //enqueue the home style 
               wp_enqueue_style('home'); 
            }
            
            if(shopp('catalog','is-product')){
                //enqueue styles for the product page
                wp_enqueue_style('cloudZoom');
                wp_enqueue_style('fancybox');
            }
        }
    }
    
    add_action('wp_print_styles','add_styles');
    
    function add_scripts(){
            
        if(!is_admin()){
            //enqueue the generic scripts
            wp_enqueue_script('jquery');
            wp_enqueue_script('main');
            wp_enqueue_script('superfish');
            wp_enqueue_script('currency');
        
            if(is_home()){
                //enqueue the home scripts
                wp_enqueue_script('home');
                wp_enqueue_script('kwickslider');
                wp_enqueue_script('easing');
            }
            
            if(shopp('catalog','is-product')){
                //enqueue scripts for the product page
                wp_enqueue_script('product');
                wp_enqueue_script('fancybox');
                wp_enqueue_script('carousel');
                wp_enqueue_script('cloudZoom');
                shopp_enqueue_script('strip_variation',get_bloginfo('template_directory')."/js/strip_variation.js",array('shopp'),'1.0',true);
            }

        //Shopp enqueue the ajax xart file in the footer
        shopp_enqueue_script('ajaxcart', get_bloginfo('template_directory')."/js/ajaxcart.js",array('shopp'),'1.0',true);
    }    
}
    
    //Wordpress Hook to enqueue scripts
    add_action('wp_print_scripts','add_scripts');
    
	// Clean up the <head>
	function removeHeadLinks() {
    	remove_action('wp_head', 'rsd_link');
    	remove_action('wp_head', 'wlwmanifest_link');
    }
    add_action('init', 'removeHeadLinks');
    remove_action('wp_head', 'wp_generator');
    
    function indian_hanger_widgets_init(){
        // Declare sidebar widget zone
        if (function_exists('register_sidebar')) {
        	register_sidebar(array(
        		'name' => 'Primary Sidebar Widgets',
        		'id'   => 'primary-sidebar-widgets',
        		'description'   => 'These are widgets for the primary sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
            
            register_sidebar(array(
        		'name' => 'Secondary Sidebar Widgets',
        		'id'   => 'Secondary-sidebar-widgets',
        		'description'   => 'These are widgets for the right sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
            
            register_sidebar(array(
        		'name' => 'Home Right Sidebar Widgets',
        		'id'   => 'home-right-sidebar-widgets',
        		'description'   => 'These are widgets for the home right sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
            
            register_sidebar(array(
        		'name' => 'Home Bottom Primary Sidebar Widgets',
        		'id'   => 'home-bottom-primary-sidebar-widgets',
        		'description'   => 'These are primary widgets for the home bottom horizontal sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
            
            register_sidebar(array(
        		'name' => 'Home Bottom Secondary Sidebar Widgets',
        		'id'   => 'home-bottom-secondary-sidebar-widgets',
        		'description'   => 'These are secondary widgets for the home bottom horizontal sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
            
            register_sidebar(array(
        		'name' => 'Footer Widgets',
        		'id'   => 'footer-widgets',
        		'description'   => 'These are footer widgets sidebar.',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        		'before_title'  => '<h2>',
        		'after_title'   => '</h2>'
        	));
        }
    }
	/** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
    add_action('widgets_init', 'indian_hanger_widgets_init');
    
    if(function_exists(register_nav_menus())){
        register_nav_menus(
            array(
                'main_nav' => 'Main Navigation Menu'
            )
        );
    }
    
    function new_breadcrumbs(){
        $breadcrumb = shopp('catalog','breadcrumb','return=true');
        $breadcrumb = preg_replace("/<a\s*href\s*=\s*(.+)\s*>\s*(Shop)\s*<\/a>/i",'<a href="'.home_url().'">Indian Hanger</a>',$breadcrumb);
        if(shopp('catalog','is-product')){
          $breadcrumb = preg_replace("/<\/ul>/",'&nbsp;&raquo; <li><a href="'.shopp('product','url','return=true').'">'.shopp('product','name','return=true').'</a></li></ul>',$breadcrumb);
        }
        return $breadcrumb;
    }
    
    function new_pagination(){
        $pages = shopp('category','pagination','show=10&return=true');
        $pages = str_replace(array('Pages:','previous','next'),array('','&laquo','&raquo'),$pages);
        $pages = preg_replace("/<li\s*class=(\'|\")(.*)disabled(\'|\")>\s*(\&raquo|\&laquo)\s*<\/li>/i","",$pages);
        //pagination links change in case of search....
        if(isset($_GET['s'])){
            $search_term = urlencode($_GET['s']);
            $pages = preg_replace('/(http:\/\/((((\w|-|\.)+)\/)+))/i','$1?s='.$search_term.'&catalog=true',$pages);
        }
        return $pages;
    }
    
    function regenerate_search(){
        
        $search = shopp('catalog','searchform', 'return=true');
        $pattern = '/<input\s*type=("|\')(submit)("|\')\s*value=("|\')Search("|\')\s*id=("|\')(searchsubmit)("|\')\s* \/>/';
        $replacement = '<button type="$2" id="$7">Search</button>';
        $search = preg_replace($pattern,$replacement,$search);
        
        return $search;
    }
    
    function get_parent_category($catergory_id){
        global $wpdb;
        
        $category = $wpdb->get_row("SELECT id,parent,name,uri FROM wp_shopp_category WHERE id={$catergory_id} LIMIT 1");
        
        return $category;
    }
    
    function get_category_id($category_name){
        global $wpdb;
        $category_id = $wpdb->get_var("SELECT id FROM wp_shopp_category WHERE name = '{$category_name}'");
        
        return $category_id;
    }
    
    function get_subcategory_list($parent_id){
        global $wpdb;
        $subcategories = $wpdb->get_results("SELECT id,name,uri from wp_shopp_category WHERE parent={$parent_id}");
        
        return $subcategories;
    }
    
    function strip_price($price){
        
        $price = utf8_decode($price);
    
        $strip_value = str_replace('?','',$price);
        
        $strip_value = str_replace(',','',$strip_value);
        
        return $strip_value;
    }
    
    function add_rupee_symbol($price){
        
        $price = strip_price($price);
        
        $price_with_symbol = "<img src='".get_bloginfo('template_directory')."/images/Rupee-symbol.png' alt='rupee_symbol' />";
        $price_with_symbol .= "&nbsp;".$price;
        return $price_with_symbol;
    }
    
    function get_conversion_rate(){
        
        global $geoplugin;
        global $current_conversion;
        
        //Get Posted data
        //$amount = $_POST['amount'];
        $from = 'INR';
        if(isset($_POST['to_currency'])){
            $to = $_POST['to_currency'];
        }else{
            if(isset($_COOKIE['currency'])){
                $to = $_COOKIE['currency'];
            }else{
                $to = $geoplugin->currencyCode;
                setcookie('currency',$to,time()+(60*60*24*365),'/');
            }
        }
         
        //make string to be put in API
        $string = "1".$from."=?".$to;
         
        //Call Google API
        $google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
         
        //Get and Store API results into a variable
        $result = file_get_contents($google_url);
        
        //Explode result to convert into an array
        $result = explode('"', $result);
        
        ################################
        # Conversion
        ################################
        
        $converted_amount = explode(' ', $result[3]);
        
        $conversion = $converted_amount[0];
        
        $currency = ucwords(str_replace($converted_amount[0],"",$result[3]));
        
        $current_conversion = array("rate" => $conversion, "currency" => $currency);
    }
    
    function convert_currency($amount){
        
        global $current_conversion;
        
        //Check if the conversion has already happened and call the API for the process only once.... 
        if(is_null($current_conversion)){
            get_conversion_rate();
        }
        
        $foreign_price = (float)$current_conversion['rate'] * $amount;
        $foreign_price = round($foreign_price);
        $return = array('total_value' => number_format($foreign_price)." ".$current_conversion['currency'],'conversion_rate' => (float)$current_conversion['rate']);
        return $return;
    }
    
    function shipping_calc() {
        global $Shopp;
        if(empty($Shopp->Order->Cart->shipped)){
            return;
        }else{
            $total_weight = 0;
            foreach($Shopp->Order->Cart->shipped as $item){
                $current_item_weight = $item->quantity * $item->weight;
                $total_weight = $total_weight + $current_item_weight;
            }
            
            $shipping_country = $Shopp->Order->Shipping->country;
            
            //load the shipping xml file as an object...
            $shipping_file = $_SERVER['DOCUMENT_ROOT']."online_store/wp-content/themes/BLANK-Theme/xml/shipping.xml";
            if (file_exists($shipping_file)){
                $shipping = simplexml_load_file($shipping_file);
            }
            
            $country_node = $shipping->xpath('/shipping/ship[to="'.$shipping_country.'"]');
            
            $all_weights = array(500,1000,1500,2000,2500);
            
            foreach($all_weights as $weight){
                if($total_weight <= $weight){
                    $current_weight_bracket = "upto_".$weight;
                    break;
                }
            }
            
            reset($all_weights);
            
            foreach($country_node[0]->rate as $rate){
                if((string)$rate['case'] == $current_weight_bracket){
                    $current_rate = (int)$rate;
                    break;
                }
            }
            
            //$country_rate = (int)$country_node[0]->rate;
            $Shopp->Order->Cart->Totals->shipping = $current_rate;
            
            $Shopp->Order->Cart->Totals->total =  $Shopp->Order->Cart->Totals->shipping + $Shopp->Order->Cart->Totals->subtotal;
        }
    }

    add_action('wp','disable_shopp_resources',100);
    
    function disable_shopp_resources() {
    
        // Disable the Shopp Colorbox implementation
        shopp_deregister_script('colorbox');
    
        wp_deregister_style('shopp.catalog');
        wp_deregister_style('shopp');
        wp_deregister_style('shopp.colorbox');
        wp_deregister_style('shopp.printable');
    
        // Remove Inline Shopp CSS
        $Storefront =& ShoppStorefront();
        if ($Storefront) {
            remove_action('wp_print_styles',array(&$Storefront, 'catalogcss'));
        }
    }    
?>

<?php

    function add_currency_dropdown(){
        //load the currency xml file as an object...
        if(stripos($_SERVER['SERVER_NAME'],'localhost') !== false){
            $currency_file = $_SERVER['DOCUMENT_ROOT']."online_store/wp-content/themes/BLANK-Theme/xml/currencies.xml";
        }else if(stripos($_SERVER['SERVER_NAME'],'recipemobile.com') !== false){
            $currency_file = $_SERVER['DOCUMENT_ROOT']."/duka/wp-content/themes/BLANK-Theme/xml/currencies.xml";
        }
        
        if (file_exists($currency_file)){
            $currencies = simplexml_load_file($currency_file);
        }
?>

            <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" id="currencyConvert" name="currency_convert">
                <!-- The select options get populated from the xml file loaded earlier -->
                <select name="to_currency" id="toCurrency">
                    
                    <?php foreach($currencies->currency as $currency): ?>
                        <option value="<?php echo $currency->code; ?>"
                            <?php if(isset($_POST['to_currency']) && $currency->code == $_POST['to_currency']  ): ?>
                                selected="selected"
                            <?php elseif($currency->code == $_COOKIE['currency']): ?>
                                selected="selected"
                            <?php endif; ?>
                        ><?php echo $currency->name; ?></option>
                    <?php endforeach; ?>
                </select>
                
                <button type="submit" name="convert" id="convert">Convert</button>
            </form>
            
            <dl id="target" class="dropdown hidden">
                <dt>
                    <a href="javascript:void(0);"></a>
                </dt>
                <dd>
                    <ul>
                        <?php foreach($currencies->currency as $currency): ?>
                            <li>
                                <a href="javascript:void(0);">
                                    <span class="flag flag-<?php echo $currency->code; ?>"></span>
                                    <span class="currency-value"><?php echo $currency->name; ?></span>
                                    <span class="value"><?php echo $currency->code; ?></span>
                                    <span class="country"><?php echo $currency->country; ?></span>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </dd>
            </dl>
<?php  
     }
?>